package com.tyk.bookstore.back.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.system.valid.annotation.AvatarValid;
import com.tyk.bookstore.back.user.model.query.UpdateBaseInfoQuery;
import com.tyk.bookstore.back.user.model.query.UpdatePasswordQuery;
import com.tyk.bookstore.back.user.model.vo.RouteVo;
import com.tyk.bookstore.back.user.model.vo.UserInfoVo;
import com.tyk.bookstore.back.user.service.UserCenterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@CrossOrigin
@Tag(name = "用户信息服务", description = "用户信息服务")
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("api/user")
public class UserCenterController {

    private final UserCenterService userCenterService;

    @Operation(summary = "获取用户已拥有菜单列表", description = "获取用户已拥有菜单列表")
    @SaCheckLogin
    @GetMapping("routes")
    public R<List<RouteVo>> getRoutes() {
        return R.ok(userCenterService.getRoutes());
    }

    @Operation(summary = "获取用户信息", description = "获取用户信息")
    @SaCheckLogin
    @GetMapping("info")
    public R<UserInfoVo> getUserInfo() {
        return R.ok(userCenterService.getUserInfo());
    }

    @Operation(summary = "修改用户头像", description = "修改用户头像")
    @SaCheckLogin
    @PutMapping("avatar")
    public R<Object> uploadAvatar(@Validated @NotNull @AvatarValid @RequestParam("avatar") MultipartFile file) {
        userCenterService.uploadAvatar(file);
        return R.ok();
    }

    @Operation(summary = "修改用户基本信息", description = "修改用户基本信息")
    @SaCheckLogin
    @PutMapping("base")
    public R<Object> updateBaseInfo(
            @Validated @RequestBody UpdateBaseInfoQuery query
    ) {
        userCenterService.updateBaseInfo(query);
        return R.ok();
    }

    @Operation(summary = "修改密码", description = "修改密码")
    @SaCheckLogin
    @PutMapping("password")
    public R<Object> updatePassword(
            @Validated @RequestBody UpdatePasswordQuery query
    ) {
        userCenterService.updatePassword(query);
        return R.ok();
    }

}
